﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;



namespace Testdb4FreeConnection
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            String[] Customers = { "Kalamazoo County", "Kent County", "Cass County", "Lake County", "Dr. Smith", "Dr. Dean", "Dr.John", "Bronson Hospital" };
            String[] Sates = { "MI","IN","OH","CA"};
            String[] County = { "Kalamazoo","Cass","Kent","Lake"};
            String[] PrimaryMarkets = { "Therpuetic", "Drugs of Abuse" };
            String[] SalesReps = { "JP","KL","MM","BD","AA","PR"};
            //String[] ParentLIMS = { };
            int LabID = 1022;
            int SpecID = 63215;
            String[] names = { "John", "Jim", "Bill", "Bob", "Jim", "Jill", "Jenny", "Hannah", "Kim", "Sam", "Garret", "George", "Jeff", "Mark"};
            String[] Results = { "P", "N", "C"};
            String[] Drugs = { "Amphetamine", "Methamphetmine", "Oxycodone", "Proponalol", "Hydrocodone", "Oxymorphone" };
            String[] Types = {"SCREEN","CONFIRM"};


            Random rnd = new Random();

            //Console.WriteLine(Customers[rnd.Next(Customers.Length)]);

            for (int i = 0; i < 1000; i++ )
            {
                CallDB("INSERT INTO FactSamples VALUES('C" + i+3
                    +"','"+ Customers[rnd.Next(Customers.Length)]
                    +"','"+ Sates[rnd.Next(Sates.Length)]
                    +"','"+ County[rnd.Next(County.Length)]
                    +"','"+ PrimaryMarkets[rnd.Next(PrimaryMarkets.Length)]
                    +"','"+ SalesReps[rnd.Next(SalesReps.Length)]
                    + "','" + "C" + (i-1)
                    +"','"+ LabID++
                    +"','"+ "S"+ SpecID++
                    + "','" + rnd.Next(2010, 2014) + string.Format("{0:00}", rnd.Next(12)) + string.Format("{0:00}", rnd.Next(30))
                    + "','" + rnd.Next(2010, 2014) + string.Format("{0:00}", rnd.Next(12)) + string.Format("{0:00}", rnd.Next(30))
                    + "','" + rnd.Next(2010, 2014) + string.Format("{0:00}", rnd.Next(12)) + string.Format("{0:00}", rnd.Next(30))
                    + "','" + names[rnd.Next(names.Length)] + " TestDonor"
                    + "','" + names[rnd.Next(names.Length)] + " TestCollector"
                    + "','" + Results[rnd.Next(Results.Length)]
                    + "','" + Drugs[rnd.Next(Drugs.Length)]
                    + "','" + Types[rnd.Next(Types.Length)]
                    + "','" + Results[rnd.Next(Results.Length)]
                    + "','" + string.Format("{0:0.00}",rnd.NextDouble() * 2+rnd.NextDouble() )
                    + "','" +  "url"
                    +"');"); 
            }
        }

        private void CallDB(String cmdtxt)
        {
            string strConnection = "host=db4free.net;user=dbadmin;password=dbadmin;database=datawarehouse490;";


            MySql.Data.MySqlClient.MySqlConnection conn;
            MySql.Data.MySqlClient.MySqlCommand cmd;

            conn = new MySql.Data.MySqlClient.MySqlConnection();
            cmd = new MySql.Data.MySqlClient.MySqlCommand();

            conn.ConnectionString = strConnection;

            try
            {
                conn.Open();
                cmd.Connection = conn;

                cmd.CommandText = cmdtxt;  //"INSERT INTO FactSamples VALUES("++");";
                cmd.Prepare();

                cmd.Parameters.AddWithValue("@number", 1);


                for (int i = 1; i <= 1; i++)
                {
                    cmd.Parameters["@number"].Value = i;

                    cmd.ExecuteNonQuery();
                }
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                MessageBox.Show("Error " + ex.Number + " has occurred: " + ex.Message,
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
